﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Image_Test1.ObjectTypes;

namespace Image_Test1
{
    /// <summary>
    /// class containing the adjustment functions (contrast , brightness)
    /// </summary>
    class Adjust
    {
        /// <summary>
        /// adjust the contrast of the image
        /// </summary>
        /// <param name="img">array of image pixels</param>
        /// <param name="newMin"></param>
        /// <param name="newMax"></param>
        /// <returns>the image after contrast adjustment</returns>
        public MyColor[,] Contrast(Bitmap img, int newMin, int newMax)
        {
            int width = img.Width;
            int height = img.Height;

            MyColor[,] image = new MyColor[width, height];
            MyColor[,] resultBuffer = new MyColor[width, height];

            ///OldMin = the minmum of R an G and B In The Given Pic
            ///OldMax = the Max of R an G and B In The Given Pic
            MyColor OldMin = image[0, 0];
            MyColor OldMax = image[0, 0];

            ///Turing The img to MyColor[,]
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    image[i, j].Red = img.GetPixel(i, j).R;
                    image[i, j].Green = img.GetPixel(i, j).G;
                    image[i, j].Blue = img.GetPixel(i, j).B;
                }
            }

            ///Find The MAX and MIN in this pic
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    #region Finding Max Min Red
                    if (img.GetPixel(i, j).R > OldMax.Red)
                    { OldMax.Red = img.GetPixel(i, j).R; }
                    if (img.GetPixel(i, j).R < OldMin.Red)
                    { OldMin.Red = img.GetPixel(i, j).R; }
                    #endregion

                    #region Finding Max Min Green
                    if (img.GetPixel(i, j).G > OldMax.Green)
                    { OldMax.Green = img.GetPixel(i, j).G; }
                    if (img.GetPixel(i, j).G < OldMin.Green)
                    { OldMin.Green = img.GetPixel(i, j).G; }
                    #endregion

                    #region Finding Max Min Blue
                    if (img.GetPixel(i, j).B > OldMax.Blue)
                    { OldMax.Blue = img.GetPixel(i, j).B; }
                    if (img.GetPixel(i, j).B < OldMin.Blue)
                    { OldMin.Blue = img.GetPixel(i, j).B; }
                    #endregion

                }
            }

            float x, y, z;
            for (int i = 0; i < width; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    Color currentPixel = img.GetPixel(i, j);
                    

                    #region Red
                    x = (currentPixel.R - OldMin.Red);
                    y = (OldMax.Red - OldMin.Red);
                    z = (newMax - newMin);
                    float red = ((x / y) * z + newMin);
                    if (red > 255)
                    {
                        red = 255;
                    }
                    else if (red < 0)
                    {
                        red = 0;
                    }
                    #endregion

                    #region Green

                    x = (currentPixel.G - OldMin.Green);
                    y = (OldMax.Green - OldMin.Green);
                    z = (newMax - newMin);
                    float green = ((x / y) * z + newMin);
                    if (green > 255)
                    {
                        green = 255;
                    }
                    else if (green < 0)
                    {
                        green = 0;
                    }
                    #endregion

                    #region Blue
                    x = (currentPixel.B - OldMin.Blue);
                    y = (OldMax.Blue - OldMin.Blue);
                    z = (newMax - newMin);
                    float blue = ((x / y) * z + newMin);
                    if (blue > 255)
                    {
                        blue = 255;
                    }
                    else if (blue < 0)
                    {
                        blue = 0;
                    }
                    #endregion

                    ///Constructing New Image Buffer (The Return Of This Function) Putting The Reslut Into The Buffer.
                    resultBuffer[i, j] = new MyColor() { Red = (byte)red, Green = (byte)green, Blue = (byte)blue };
                }
            }
            return resultBuffer;
        }


        /// <summary>
        /// set the brightness degree of the image
        /// </summary>
        /// <param name="img">array of image pixels</param>
        /// <param name="offset">degree of brightness</param>
        /// <returns>the image after brightness effect</returns>
        public Bitmap brightness(MyColor[,] img, int offset)
        {
            int height = img.GetLength(1);
            int width = img.GetLength(0);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    img[i, j].Red = (img[i, j].Red) + offset;
                    if (img[i, j].Red > 255)
                        img[i, j].Red = 255;
                    else if (img[i, j].Red < 0)
                        img[i, j].Red = 0;
                    img[i, j].Green = (img[i, j].Green) + offset;
                    if (img[i, j].Green > 255)
                        img[i, j].Green = 255;
                    else if (img[i, j].Green < 0)
                        img[i, j].Green = 0;
                    img[i, j].Blue = (img[i, j].Blue) + offset;
                    if (img[i, j].Blue > 255)
                        img[i, j].Blue = 255;
                    else if (img[i, j].Blue < 0)
                        img[i, j].Blue = 0;
                }
            }
            Bitmap Result = new Bitmap(img.GetLength(0), img.GetLength(1));
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Result.SetPixel(i, j, Color.FromArgb(img[i, j].Red, img[i, j].Green, img[i, j].Blue));
                }
            }
            return Result;
        }
    }

}
